import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
// let car = uni.getStorageSync('lph-shopCar') || [];
let storeInfo1 = uni.getStorageSync('lph-storeInfo') || {};

let $format = (val) => { // 处理金额
	let str = val.toFixed(2) + '';
	let intSum = str.substring(0, str.indexOf(".")).replace(/\B(?=(?:\d{3})+$)/g, ',');
	let dot = str.substring(str.length, str.indexOf("."));
	let ret = intSum + dot;
	return ret;
}

const store = new Vuex.Store({
	state: {
		isLogin: false,
		sList: [], // 搜索历史
		storeInfo: storeInfo1, // 店铺的信息
		userInfo: {},
		shname: '',
		withdraw: {},
		shDetails: {
			shavatar: '/static/imgs/comment-icon2.png'
		},
		shopCart: [], // 购物车
		payData: [], // 支付数据
		delData: {}, // 删除的数据
		allFlag: false, // 全选
		orderData: {} ,//订单数据
	},

	mutations: {
		setOrderData(state,d) { //设置订单数据
			state.orderData = d
		},
		allSelectS(state,flag){ // 底部全选
			state.shopCart.forEach((shop) => {
				shop.shopSelect = flag;
				state.allFlag = flag;
				shop.itemInfo.forEach((goods) => {
					goods.goodsSelect = flag;
				})
			})
		},
		
		xshopSelect(state, u) { // 店铺勾选
			let a = []; // 店铺的所有勾选
			state.shopCart.forEach((shop) => {
				if (shop.shop_id == u.shop_id) {
					shop.shopSelect = u.selected;
					shop.itemInfo.forEach((goods) => {
						goods.goodsSelect = u.selected;
					})
				}
				a.push(shop.shopSelect);
			})
			let iii = a.some((item) => {
				return item == false;
			})
			state.allFlag = !iii;
			
		},
		updateCart(state, cart) { // 更新购物车
			console.log(cart);
			state.shopCart = cart
		},
		addCart(state, goodsInfo) { // 添加购物车
			// 1.先判断是否有店铺 - 存在  -不存在
			// 店铺不存在 - 传过来的数据push进去
			// 店铺存在 - 就已经有商品数据
			// 判断商品是否存在
			let shopFlag = false;
			let goodsFlag = false;
			state.shopCart.some((shop) => {
				if (shop.shopid == goodsInfo.shopid) { // 店铺存在
					shopFlag = true; // 店铺存在
					shop.itemInfo.some((goods) => {
						if (goods.proid == goodsInfo.itemInfo[0].proid) { // 商品存在
							goods.num += parseInt(goodsInfo.itemInfo[0].num);
							goodsFlag = true;
							return true;
						}
					});
					if (!goodsFlag) { // 商品不存在
						shop.itemInfo.push(goodsInfo.itemInfo[0]);
						return true;
					}
				}
			})

			if (!shopFlag && !goodsFlag) { // 店铺 商品 都不存在
				state.shopCart.push(goodsInfo);
			}
		},
		updateNum(state, u) { // 商品数量变更
			state.shopCart.some((shop) => {
				if (shop.shop_id == u.shop_id) { // 确认店铺
					shop.itemInfo.some((goods) => {
						if (goods.product_id == u.product_id) { // 确认商品
							goods.product_quantity = parseInt(u.product_quantity);
							return true;
						}
					});
				}
			})
			// uni.setStorageSync('lph-car', state.shopCart);
		},

		updateAddress(state, u) { // 地址变更
			state.shopCart.some((shop) => {
				if (shop.shop_id == u.shop_id) { // 确认店铺
					shop.delivery_name = u.delivery_name;
					shop.delivery_type = u.delivery_type;
					return true;
				}
			})
			// uni.setStorageSync('lph-car', state.shopCart);
		},

		updataSelect(state, u) { // 商品选择变更
			let dd = []; //总勾选
			
			state.shopCart.forEach((shop) => {
				if (shop.shop_id == u.shop_id) { // 确认店铺
					shop.itemInfo.some((goods) => {
						if (goods.product_id == u.product_id) { // 确认商品
							goods.goodsSelect = u.goodsSelect;
						}
					});
				}
				let allSelect = []; //店铺内商品的勾选情况
				shop.itemInfo.forEach((goods) => { // 勾选店铺
					allSelect.push(goods.goodsSelect);
				})
				dd = dd.concat(allSelect)
				let iii = allSelect.some((item) => {
					return item == false;
				})
				let zzz = dd.some((item) => {
					return item == false;
				})
				shop.shopSelect = !iii;
				state.allFlag = !zzz;
			})
		},

		delStoreGoods(state, d) { // 获取 删除当前店铺的所有商品
			let dGoods = {
				shopid: 1,
				goodsId: [1, 2]
			}
			let cc = {
				shopid: '',
				goodsId: []
			}; // 删除的数据
			state.shopCart.forEach((shop, index) => { // 店铺
				let SNum = d.totalPrice[shop.shop_id];
				if (shop.shop_id == d.shop_id && SNum > 0) { // 商品 
					console.log(SNum);
					shop.itemInfo.forEach((goods) => {
						if (goods.goodsSelect) {
							cc.goodsId.push(goods.product_id + '')
						}
					})

				}
				cc.shopid = d.shop_id;
			})
			console.log(cc);
			state.delData = cc;
		},

		// delGoods(state,shop_id){ // 删除本地
		// 	let a = state.delData;
		// 	let gID = a.goodId || [];
		// 	console.log(gID);
		// 	state.shopCart.forEach((store) => {
		// 		console.log(store);
		// 		if(store.shop_id == shop_id) {
		// 			store.itemInfo.forEach((goods,index) => {
		// 				console.log(1);
		// 				console.log(goods.product_id);
		// 				console.log(goods);
		// 				if(gID.indexOf(goods.product_id) != -1) {
		// 					console.log(1);
		// 				}
		// 			})
		// 		}

		// 	})
		// 	state.delData
		// },
		changeSList(state, u) { // 更改搜索历史
			state.sList = u
		},
		delSList(state) { // 删除搜索历史
			state.sList = []
		},

		changePayData(state, u) { // 更改支付数据
			state.payData = u;
			console.log(state.payData);
		},
		changePayPattern(state, u) { //修改支付方式
			state.payData.pay_tpye = u;
		},

		setWithDraw(state, val) { // 提现数据
			state.withdraw = val
		},

		changeStoreInfo(state, val) { // 更改店铺信息
			let name = val.name;
			let msg = val.msg;
			console.log(name);
			console.log(msg);

			state.storeInfo.name = msg; // vuex 改变
			console.log(state.storeInfo);
			// console.log(state.storeInfo);
			uni.setStorageSync('lph-storeInfo', state.storeInfo); //缓存中改变
		},

		setShDetails(state, shDetails) { // 设置店铺信息
			state.shDetails = shDetails
		},

		changeShDetails(state, value) { // 更改店铺信息
			state.shDetails[value[0]] = value[1]
		},
		changeShName(state, shname) { // 更改店铺名
			state.shname = shname
			console.log('state:' + state.shname)
		},
		changeLogin(state, res) { // 改变登录状态
			state.isLogin = res
		},
		changeUserInfo(state, res) { // 改变用户信息
			state.userInfo = res
		},
		cLogin(state, demo1) {
			state.isLogin = demo1;
			uni.setStorage({
				key: 'isLogin',
				data: demo1
			})
		},
		login(state, provider) {

			state.hasLogin = true;
			state.userInfo = provider;

			uni.setStorage({ //缓存用户登陆状态
				key: 'userInfo',
				data: provider
			})
			console.log(state.userInfo);
		},
		logout(state) {
			state.hasLogin = false;
			state.userInfo = {};
			uni.removeStorage({
				key: 'userInfo'
			})
		}
	},
	actions: {

	},
	getters: {
		totalPrice1(state) {
			// 先计算勾选的每个店铺的数量与总价
			// 再返回总的
			let allPrice = {}; // 不满减的价格
			let totalNum = {}; // 数量
			let discountPrice = {}; // 满减后价格
			let cutsPrice = {}; // 减价格
			state.shopCart.forEach((shop) => { // 每个店铺
				let total = 0; // 总价
				let num = 0; // 总数
				let cutPrice = 0; //  减价格
				shop.itemInfo.forEach((goods) => { // 每个商品
					if (goods.goodsSelect) {
						total += Math.abs(goods.product_quantity * (goods.product_price * 100) / 100);
						num += goods.product_quantity;
					}
				})
				let countPrice = total; // 满减后价格
				let a = shop.benefit || [];
				a.some((item) => { // 处理满减
					if (Number(item.addgift_amout_srtart) <= total) {
						// countPrice = parseFloat($format(total - item.addgift_discount))
						countPrice = total - parseFloat(item.addgift_discount);
						// countPrice = total;
						cutPrice = parseFloat(item.addgift_discount);
					}
				});
				allPrice[shop.shop_id] = total;
				totalNum[shop.shop_id] = num;
				discountPrice[shop.shop_id] = countPrice;
				cutsPrice[shop.shop_id] = cutPrice;
			});
			let b = [allPrice, totalNum, discountPrice, cutsPrice];
			console.log(b);
			return b;

		},
		
		totalPrice(state) {
			// 参与满减的商品
			// 不参与满减的商品
			
			// 不参与满减
			let notTotalNum = {};
			let notTotalPrice = {};
			state.shopCart.forEach((shop) => { // 每个店铺
				let notNum = 0; // 不满减的数量
				let notPrice = 0; //不满减的价格
				shop.itemInfo.forEach((goods) =>{ // 每个商品
					if (goods.goodsSelect && goods.addgift == 2){
						notPrice += Math.abs(goods.product_quantity * (goods.product_price * 100) / 100);
						notNum += goods.product_quantity;
					}
				})
				notTotalPrice[shop.shop_id] = notPrice;
				notTotalNum[shop.shop_id] = notNum;
			})
			
			let not = [notTotalPrice,notTotalNum]
			// console.log(notTotalNum);
			// console.log(notTotalPrice);
			// 先计算勾选的每个店铺的数量与总价
			// 再返回总的
			
			// 参与满减的商品
			let allPrice = {}; // 不满减的价格
			let totalNum = {}; // 数量
			let discountPrice = {}; // 满减后价格
			let cutsPrice = {}; // 减价格
			state.shopCart.forEach((shop) => { // 每个店铺
				let total = 0; // 总价
				let num = 0; // 总数
				let cutPrice = 0; //  减价格
				shop.itemInfo.forEach((goods) => { // 每个商品
					if (goods.goodsSelect && goods.addgift == 1) {
						total += Math.abs(goods.product_quantity * (goods.product_price * 100) / 100);
						num += goods.product_quantity;
					}
				})
				let countPrice = total; // 满减后价格
				let a = shop.benefit || [];
				a.some((item) => { // 处理满减
					if (Number(item.addgift_amout_srtart) <= total) {
						// countPrice = parseFloat($format(total - item.addgift_discount))
						countPrice = total - parseFloat(item.addgift_discount);
						cutPrice = parseFloat(item.addgift_discount);
					}
				});
				allPrice[shop.shop_id] = total;
				totalNum[shop.shop_id] = num;
				discountPrice[shop.shop_id] = countPrice;
				cutsPrice[shop.shop_id] = cutPrice;
			});
			let endPrice = {};
			let endNum = {};
			for (let p in discountPrice) { // 满减的	
				for (let np in notTotalPrice) { // 不满减的
					if(p == np) {
						endPrice[np] = (discountPrice[p] * 10000 + notTotalPrice[np] * 10000) / 10000;
					}
				}
			}
			for (let p in totalNum) { // 满减的
				for (let np in notTotalNum) { // 不满减的
					if(p == np) {
						endNum[np] = (totalNum[p]  + notTotalNum[np]);
						
					}
				}
			}
			
			
			let b = [allPrice, totalNum, discountPrice, cutsPrice,notTotalPrice,notTotalNum,endPrice,endNum];
			
			
			return b;
		
		},
		
		getAllSellect(state) { // 勾选中的全部状态
			let a = {
				bottom: false,
				shop: [
					{1: false},
					{2: false},
				]
			}
			let b = {
				bottom: state.allFlag,
				shop: []
			};
			state.shopCart.forEach((shop) => {
				if(state.allFlag) {
					let shopSel = {}
					shop.itemInfo.forEach((goods) => { // 找到每一个店铺
						shopSel[goods.product_id] = true;
					})
					b.shop.push(shopSel)
				}
			})
			
			return a
		},
		
		// delData(state) { // 删除的数据

		// },

		orderPrice: (state) => { // 订单列表的数据

			return '1';
		}
	}
})

export default store
